package de.farbstreich.tozen2.client;

import java.io.BufferedWriter;
import java.io.IOException;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;

import de.farbstreich.tozen2.common.TozenMessageType;
import de.farbstreich.tozen2.common.TozenProtocolMessage;

public class ClientPingThread extends Thread {

	private BufferedWriter output;
	private ClientSocket socket;

	public ClientPingThread(ClientSocket clientSocket) {
		this.output = clientSocket.outputStream;
		this.socket = clientSocket;
	}

	@Override
	public void run() {
		try {
			while(true) {
				sendPing();
				try {
					ClientPingThread.sleep(4000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		catch(IOException e)
		{
			socket.closeSocket();
			System.out.println(e);
		}
	}
	
	public void sendPing() throws ParseException, IOException {
		TozenProtocolMessage protocol = new TozenProtocolMessage(TozenMessageType.PING);
		output.write(protocol.toString());
		output.newLine();
		output.flush();
	}

	
}
